package com.example.mystore.mapper;


import com.example.mystore.model.Trolley;
import org.apache.ibatis.annotations.*;


import java.util.List;

@Mapper
public interface TrolleyMapper {

    @Insert("INSERT INTO shopping_trolley (user_id, product_id, quantity, created, updated) " +
            "VALUES (#{userId}, #{productId}, #{quantity}, #{created}, #{updated})")
    void addTrolley(Trolley trolley);


    @Select("SELECT * FROM shopping_trolley WHERE user_id = #{id}")
    @Results(id = "shoppingTrolleyResultMap", value = {
            @Result(property = "id", column = "id"),
            @Result(property = "userId", column = "user_id"),
            @Result(property = "productId", column = "product_id"),
            @Result(property = "quantity", column = "quantity"),
            @Result(property = "created", column = "created"),
            @Result(property = "updated", column = "updated")
    })
    List<Trolley> listByUserId(Integer id);

    @Update("UPDATE shopping_trolley SET quantity = #{quantity} WHERE id = #{id}")
    void changeQuantity(Integer id, Integer quantity);

    @Delete("DELETE FROM shopping_trolley WHERE id = #{id}")
    void deleteById(Integer id);


    @Delete("DELETE FROM shopping_trolley WHERE user_id = #{id}")
    void clearById(Integer id);
}

